package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.BulletinReadLogic;
import cn.topevery.garden.entity.dto.BulletinReadDTO;
import cn.topevery.garden.entity.query.BulletinReadQuery;
import cn.topevery.garden.entity.vo.BulletinReadVO;
import cn.topevery.garden.entity.po.BulletinRead;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 公共阅读状态
 */
@Api(tags = "公共阅读状态")
@RestController
@RequestMapping("/bulletinRead")
public class BulletinReadController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    BulletinReadLogic bulletinReadLogic;


    /**
     * 获取公共阅读状态分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取公共阅读状态页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<BulletinReadVO>> listPage(@RequestBody BulletinReadQuery query) {
        return JsonResult.ok(bulletinReadLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 公共阅读状态信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody BulletinReadDTO dto) {
        bulletinReadLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, BulletinRead.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<BulletinReadVO> getInfoById(@RequestParam Integer id) {
        BulletinRead entity = bulletinReadLogic.getEntity(id);
        BulletinReadVO vo= mapper.map(entity, BulletinReadVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        bulletinReadLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出公共阅读状态信息")
    @GetMapping("/export")
    public void export(BulletinReadQuery query, HttpServletResponse response) {
        List<BulletinReadVO> list = bulletinReadLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "公共阅读状态", "sheet1", BulletinReadVO.class, "公共阅读状态", response);
    }

}
